﻿//======================================================================
//
//        Copyright (C) 苏州逸仙软件    
//        All rights reserved
//
//        filename :BillBoard_E.aspx.cs
//        description :广告位编辑页面
//
//        created by 邹兴武 at  2010-12-31
//
//======================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace huaguwang.hgmanage.Advert
{
    public partial class BillBoard_E : System.Web.UI.Page
    {
        private readonly D_T_Billboard_Exp _dal = new D_T_Billboard_Exp();
        protected void Page_Load(object sender, EventArgs e)
        {
            //用来初始化用户的按钮,执行此语句的时候,but并没有具体产生
            WebUserToolBar1.EditInit(
               UserConst.Exp_GetUser,
               UserConst.Exp_GetButtonList,
               SetBut,
              UserConst.Exp_ToolBarErr
            );
            if (!IsPostBack)
            {
                InitLoad();
                if (Request.Params["id"] != null)
                {
                    ViewState.Add("id", Request.Params["id"]);
                    InitData(Request.Params["id"].Exp_IntTryParse());
                }
            }
        }

        private void InitLoad()
        {
            this.dropType.Items.Clear();
            this.dropType.Items.Add(new ListItem("请选择", ""));
            this.dropType.SelectedIndex = 0;
            foreach (string item in Enum.GetNames(typeof(M_T_BillBoard_Exp.AdvertType)))
            {
                this.dropType.Items.Add(item);
            }

            this.dropModule.Items.Clear();
            this.dropModule.Items.Add(new ListItem("请选择", ""));
            this.dropModule.SelectedIndex = 0;
            foreach(var item in Enum.GetNames(typeof(M_T_BillBoard_Exp.WebModule)))
            {
                this.dropModule.Items.Add(item);
            }
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData(int id)
        {
            var model = _dal.GetModel(id);
            if (model == null)
            {
                var SKey = this.WebUserToolBar1.EditSKey;
                this.Exp_PageAlterHref("没有找到可编辑的资料!", "billboard.aspx?" + SKey);
                return;
            }
            this.txtName.Text = model.Title;
            this.dropType.Text = model.AdType;
            this.dropModule.Text = model.Module;
            this.txtPlate.Text = model.Plate;
            this.txtPosition.Text = model.Position;
            this.txtPrice.Text = ((int)model.Price).Exp_Trim();
            this.txtSize.Text = model.Rules;
            this.rblState.Text = model.State ? "1" : "0";//1空闲 0 占用
        }

        private void SetBut(int i, Button but)
        {
            switch (i)
            {
                case 1:
                    but.CausesValidation = false;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 保存按钮事件
        /// </summary>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var model = CreateModel();
                if (model == null)
                {
                    return;
                }
                var iRet = 0;
                Log_Type le = Log_Type.add;
                if (model.id > 0)
                {
                    var tempModel = _dal.GetModel(model.id);
                    if (tempModel == null || !tempModel.Flag)
                    {
                        this.Exp_PageAlter("当前信息不存在,可能已经被其他人删除!");
                        return;
                    }
                    iRet = _dal.Update(model);
                    le = Log_Type.update;
                }
                else
                {
                    iRet = _dal.Add(model);
                }

                if (iRet > 0)
                {
                    //写日志
                    this.Exp_PageWriteLog(le, "广告位信息", iRet);
                    var SKey = this.WebUserToolBar1.EditSKey;
                    this.Exp_PageAlterHref("操作成功", "billboard.aspx?" + SKey);
                }
                else
                {
                    this.Exp_PageAlter("操作失败");
                }
            });
        }

        /// <summary>
        /// 获取model对象
        /// </summary>
        private M_T_Billboard CreateModel()
        {
            var model = new M_T_Billboard();
            model.id = ViewState["id"].Exp_IntTryParse();
            model.AdType = this.dropType.Text.Exp_Trim();
            model.EditDate = DateTime.Now;
            model.Module = this.dropModule.Text;
            model.Plate = this.txtPlate.Text.Exp_Trim();
            model.Position = this.txtPosition.Text.Exp_Trim();
            model.Price = this.txtPrice.Text.Exp_DecimalTryParse();
            model.Rules = this.txtSize.Text.Exp_Trim();
            model.Flag = true;
            model.Title = model.Module + model.Plate + model.Position + "的" + model.AdType;
            model.State = this.rblState.Text == "1";
            model.UserID = this.Exp_GetUser().id;

            return model;
        }

        /// <summary>
        /// 返回按钮事件
        /// </summary>
        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            var SKey = this.WebUserToolBar1.EditSKey;
            Response.Redirect("billboard.aspx?" + SKey);
        }
    }
}
